package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.goods.entity.spu.ExtSpu;
import com.hoshiicloud.goods.entity.spu.SpuSalesCategory;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.category.SpuSalesCategoryMapper;
import com.hoshiicloud.goods.service.SpuSalesCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 营销类目 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class SpuSalesCategoryServiceImpl extends ServiceImpl<SpuSalesCategoryMapper, SpuSalesCategory> implements SpuSalesCategoryService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @param id
     * @return com.hoshiicloud.goods.entity.SpuSalesCategory
     * @description: 根据id查询营销类目
     */
    @Override
    @Cacheable(cacheNames = "SpuSalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品管理-营销类目-查询营销类目 ")
    public SpuSalesCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增营销类目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目-新增营销类目")
    public boolean save(SpuSalesCategory entity) {

        // 主键ID生成
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改营销类目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-修改营销类目")
    public boolean updateById(SpuSalesCategory entity) {

        // 更新数据
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     * @param queryWrapper
     * @return IPage
     * @description: 分页查询营销类目
     */
    @Override
    @Logable(authName = "商品管理-营销类目管理-分页查询营销类目")
    @Cacheable(cacheNames = "SpuSalesCategory", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5(#root.targetClass.simpleName+':'+#root.methodName+':'+#key)")
    public IPage getPage(IPage page, Wrapper queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-删除营销类目")
    public boolean deleteById(Long id) {

        // 根据id查询数据
        SpuSalesCategory entity = this.getById(id);
        if (entity == null) {
            log.error("删除营销类目异常，无法找到营销类目记录");
            throw new ServiceException("删除营销类目异常，无法找到营销类目记录");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "SpuSalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-营销类目管理-根据id集合查询营销类目")
    public List<SpuSalesCategory> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public List<SpuSalesCategory> getByParam(Wrapper wrapper) {
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<SpuSalesCategory> listBySpuIds(List<Long> spuIds) {
        QueryWrapper<SpuSalesCategory> q = new QueryWrapper<>();
        q.lambda().in(SpuSalesCategory::getExtSpuId,spuIds).eq(SpuSalesCategory::getDeleted,0);
        return baseMapper.selectList(q);
    }

    @Override
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-批量保存营销类目")
    public Boolean saveBatch1(List<SpuSalesCategory> list) {
        return this.saveBatch(list);
    }

    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "根据自定义条件批量更新")
    @Override
    public boolean updateBatchByWrapper(UpdateBatchParam<SpuSalesCategory> updateBatchParam) {
        SpuSalesCategory updateEntity = updateBatchParam.getUpdateEntity();
        QueryWrapper<SpuSalesCategory> wrapper = updateBatchParam.getWrapper();
        return this.update(updateEntity, wrapper);
    }

    /**
     * 批量更新
     */
    @Override
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "批量更新")
    public Boolean updateBatch(OmnipotentParam<SpuSalesCategory> param) {
        OmnipotentWrapper<SpuSalesCategory> wrapper = new OmnipotentWrapper<>();
        UpdateWrapper<SpuSalesCategory> updateWrapper = wrapper.getUpdateWrapper(param.getWrappers());
        SpuSalesCategory entity = param.getEntity();
        return this.update(entity, updateWrapper);
    }
}
